package com.songlanyun.modules.singlegoods.controller.admin;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.drive.Validator;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.modules.singlegoods.model.ChangeStockDTO;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.singlegoods.entity.ProductSingleGoodsEntity;
import com.songlanyun.modules.singlegoods.service.ProductSingleGoodsService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;



/**
 * 商家的单品表，用来管理库存
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-04 09:03:01
 */
@RestController
@RequestMapping("admin/singlegoods")
public class ProductSingleGoodsAdminController extends AbstractController {
    @Autowired
    private ProductSingleGoodsService productSingleGoodsService;


    /**
    * 分页查询单品
    * @param params
    * params参数说明
    * shop_id 商家店铺id必给
    * goods_group_id 商家店实商品分组id（可以不给，给只能给二级）
    * title 单品标题，可以不给
    * code 单品编码，可以不给
    * page 当前第几页（默认第一页）
    * limit 每页多少条 （默认每页10条）
     */
    @RequestMapping("/pages")
    //@RequiresPermissions("shop:singlegoods:list")
    public R pages(@RequestParam Map<String, Object> params){
        params.put("shop_id",this.getShopId());
        PageUtils page = productSingleGoodsService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 商品单品信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("shop:singlegoods:info")
    public R info(@PathVariable("id") Long id){
		ProductSingleGoodsEntity productSingleGoods = productSingleGoodsService.getByShopIdAndId(getShopId(),id);
        return R.ok().put("data", productSingleGoods);
    }

    /**
     * 保存
     */
    @SysLog("保存单品")
    @RepeatSubmit(value = "5")
    @RequestMapping("/create")
    //@RequiresPermissions("shop:singlegoods:create")
    public R create(@RequestBody ProductSingleGoodsEntity productSingleGoods){
        ProductSingleGoodsEntity obj = productSingleGoodsService.add(getShopId(), productSingleGoods);
        return R.ok().put("data", obj);
    }

    /**
     * 修改
     */
    @SysLog("修改单品")
    @RepeatSubmit(value = "5")
    @RequestMapping("/update")
    //@RequiresPermissions("shop:singlegoods:update")
    public R update(@RequestBody ProductSingleGoodsEntity productSingleGoods){
        ProductSingleGoodsEntity obj = productSingleGoodsService.update(getShopId(), productSingleGoods);
        return R.ok().put("data", obj);
    }


    /**
     * 删除
     */
    @SysLog("删除单品")
    @DeleteMapping("/delete/{id}")
    //@RequiresPermissions("shop:singlegoods:delete")
    public R delete(@PathVariable("id") Long id){
        productSingleGoodsService.delete(this.getShopId(),id);
        return R.ok();
    }

    /**
     * 上架下架
     */
    @SysLog("上架下架单品")
    @RepeatSubmit(value = "5")
    @PutMapping("/added/{id}")
    //@RequiresPermissions("shop:singlegoods:delete")
    public R added(@PathVariable("id") Long id){
        productSingleGoodsService.added(this.getShopId(),id);
        return R.ok();
    }

    /**
     * 手动增加库存
     * @param dto
     * @return
     */
    @SysLog("手动增加单品库存")
    @RepeatSubmit(value = "5")
    @PostMapping("/manual/in_stock")
    //@RequiresPermissions("shop:singlegoods:manualInStock")
    public R manualInStock(@RequestBody ChangeStockDTO dto){
        ValidatorUtils.validateEntity(dto);
        productSingleGoodsService.manualInStock(getShopId(), dto.getId(),dto.getStock());

        return R.ok();
    }

    /**
     * 手动减少库存(库存不足会报错)
     * @param dto
     * @return
     */
    @SysLog("手动减少单品库存")
    @RepeatSubmit(value = "5")
    @PostMapping("/manual/out_stock")
    //@RequiresPermissions("shop:singlegoods:manualOutStock")
    public R manualOutStock(@RequestBody ChangeStockDTO dto){
        ValidatorUtils.validateEntity(dto);
        productSingleGoodsService.manualOutStock(getShopId(), dto.getId(),dto.getStock());
        return R.ok();
    }




}
